package com.cloudsigns.factorymanager2.engineering.model;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by tony on 2018/5/30.
 */

public class PublicationQuestionFeedBackModel implements IPublicationQuestionFeedBackModel {

    @Override
    public void publicationQuestionFeedBack(final String engineeringPublicationMediaId, final String orderId, final List<String> feedbackPicList, final String unusualReason, final OnPublicationQuestionFeedBackListener publicationQuestionFeedBackListener) {
        new Thread(new Runnable() {
            public void run() {

                try {
                    HttpClient client = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(ProjectUtil.PUBLICATION_QUESTION_FEEDBACK);

                    HttpResponse response;

                    // 添加http头信息
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);

                    JSONObject jsonObject = new JSONObject();

                    jsonObject.put("itemId", engineeringPublicationMediaId);
                    jsonObject.put("orderId", orderId);
                    jsonObject.put("unusualReason", unusualReason);

                    if (null != feedbackPicList) {
                        JSONArray picJsonArr = new JSONArray();
                        for (String url : feedbackPicList) {
                            picJsonArr.put(url);
                        }
                        jsonObject.put("feedbackPicList", picJsonArr);

                    }

                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);


                    httpPost.setEntity(se);


                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != publicationQuestionFeedBackListener) {

                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            publicationQuestionFeedBackListener.publicationQuestionFeedBackSuccessed();
                        } else {

                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }


                            publicationQuestionFeedBackListener.publicationQuestionFeedBackFailed(error_str);


                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }

            }
        }).start();
    }

    public interface OnPublicationQuestionFeedBackListener {
        void publicationQuestionFeedBackSuccessed();

        void publicationQuestionFeedBackFailed(String desc);
    }
}
